<template>
    <!-- ref帮助获取dom 来使用better-scroll -->
    <div class="list" ref="wrapper">
        <!-- 使用better-scroll时 默认只会处理第一个子元素，所以要包裹 -->
        <div>
            <div class="area">
                <div class="title border-topbottom">当前城市</div>
                <div class="button-list">
                    <div class="button-wrapper">
                    <div class="button">{{this.currentCity}}</div>  
                    </div>
                </div>
            </div>
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="button-list">
                    <div 
                        class="button-wrapper" 
                        v-for="item of hotCities" 
                        :key="item.id"
                        @click="handleCityClick(item.name)"
                    >
                    <div class="button">{{item.name}}</div>  
                    </div>
                </div>
            </div>
            <!-- 这里给对象用v-for，item表示值，index表示键 -->
            <div 
                class="area" 
                v-for="(item, key) of cities" 
                :key="key"
                :ref='key'
            >
                <div class="title border-topbottom">{{key}}</div>
                <div class="item-list">
                    <div 
                        class="item border-bottom"
                        v-for='innerItem of item'  
                        :key="innerItem.id"
                        @click="handleCityClick(innerItem.name)"
                    >
                        {{innerItem.name}}
                    </div>
                </div>
            </div>
        </div>
        
    </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
    name: "CityList",
    props:{
        // 接收数据的默认类型
        hotCities: Array,
        cities: Object,
        letter: String
    },
    computed: {
        ...mapState({
            // 将vuex中city的对应数据传给currentCity
            currentCity : 'city'
        })
    },
    methods : {
        handleCityClick(city){
            // 使用vuex里面的方法 dispatch
            // 这里可以直接越过actions，使用commit直接调用mutations
            this.changeCity(city)
            this.$router.push('./')
        },
        // 下面这一行代码使得changeCity就直接代表了vuex中的changeCity方法，
        // 就直接可以将上面的this.$store.commit('changeCity', city)简化为this.changeCity(city)
        ...mapMutations(['changeCity'])
    },
    mounted () {
        this.scroll = new Bscroll(this.$refs.wrapper, {click: true})
    },
    // 解决了使用ajax数据后依然能滚动的问题
    updated (){
        this.scroll && this.scroll.refresh();
    },
    // vue自带组件监听器 监听letter变化
    watch : {
        letter () {
            // 要将对应字母的区域显示出来
            if (this.letter){
                const element = this.$refs[this.letter][0]
                // console.log(element)
                this.scroll.scrollToElement(element)
            }
        }
    }
}
</script>

<style lang='stylus' scoped>
    @import '~styles/varibles.styl'
    .list
        // 给list设置区域，避免把滚动时把头部挤出
        overflow: hidden
        position: absolute
        top: 1.58rem
        left: 0
        right: 0
        bottom: 0
        .border-topbottom
        // 显示一像素边框的颜色 设置伪元素 当前层级 &:before = .border-topbottom:before
            &:before
                border-color: #ccc
            &:after
                border-color: #ccc
        .title
            line-height: .54rem
            background: #eee
            padding-left: .2rem
            color: #666
            font-size: .26rem
        .button-list
            padding: .1rem .6rem .1rem .1rem
            // 触发bfc消除后面float影响
            overflow: hidden
            .button-wrapper
                float: left
                width: 33.33%
                .button
                    text-align: center
                    padding: .1rem 0
                    border-radius: .06rem   
                    margin: .1rem
                    border: .02rem solid #ccc
        .item-list
            .item
                line-height: .76rem
                padding-left: .2rem
            .border-bottom
            // 显示一像素边框的颜色
                &:before
                    border-color: #ccc
                    // border-width: .06rem

</style>